package caidan;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JPanel;
import javax.swing.JTextField;

import caidan.data.FoodData;
import caidan.data.FoodMaterial.FoodMaterialPanel;
import caidan.data.FoodProperty.FoodPropertyPanel;
import jinyilw.swing.FontTools;
import jinyilw.common.TextTools;
import jinyilw.swing.visuals.Icomp.IRichTextLabel;
import jinyilw.swing.visuals.Jcomp.JImageComp;
import jinyilw.swing.visuals.Jcomp.TransRichButton;
import jinyilw.swing.visuals.Rcomp.RDialog;
import jinyilw.swing.visuals.border.RTitledBorder;
import jinyilw.swing.visuals.layout.OneRowLayout;

public class SetFoodDataDialog extends RDialog
{
	private static final long serialVersionUID = 1699269513721511140L;
	private static Dimension buttonSize = new Dimension(75, 60);
	protected final TransRichButton save = new TransRichButton("保存");
	protected final TransRichButton cancel = new TransRichButton("取消");
	protected final FoodData oldFoodData = new FoodData(null);
	protected FoodData foodData;
	protected File dataFile;

	private Container mainContainer, buttonContainer;
	protected JImageComp imageComp;
	private JPanel namePanel;
	protected FoodPropertyPanel propertyPanel;
	protected FoodMaterialPanel materialPanel;
	protected JTextField nameField;
	private IRichTextLabel nameLabel;

	public SetFoodDataDialog(Window w)
	{
		super(w);
		init();
	}

	public SetFoodDataDialog(Window owner, ModalityType modalityType)
	{
		super(owner, modalityType);
		init();
	}

	private void init()
	{
		setSize(950, 580);
		setTitle("菜单设置");
		setTitleHeight(38);
		setTitleColor(Color.green);
		setTitleFont(FontTools.getCustomFont(22));
		setLocationRelativeTo(null);
		createGUI();
		initLayout();
		initListeners();
	}

	private void createGUI()
	{
		mainContainer = new Container();
		buttonContainer = new Container();
		imageComp = new JImageComp();
		namePanel = new JPanel(new OneRowLayout());
		propertyPanel = new FoodPropertyPanel();
		materialPanel = new FoodMaterialPanel();
		nameField = new JTextField(10);
		nameLabel = new IRichTextLabel("名称设置：");
		nameLabel.setForeground(Color.yellow);
		Font font = FontTools.getCustomFont(17);
		namePanel.setBorder(
				new RTitledBorder("  ", FontTools.tinyFont, Color.yellow));
		imageComp.setBorder(new RTitledBorder("菜单图片", font, Color.yellow));
		propertyPanel.setBorder(new RTitledBorder("属性设置", font, Color.yellow));
		materialPanel
				.setBorder(new RTitledBorder("材料设置（单人食量）", font, Color.yellow));
		nameField.setFont(font);
		nameLabel.setFont(font);

		save.setFont(FontTools.getBigFont());
		save.setForeground(Color.orange);
		cancel.setFont(FontTools.getBigFont());
		cancel.setForeground(Color.orange);
		save.setPreferredSize(buttonSize);
		cancel.setPreferredSize(buttonSize);
	}

	private void initLayout()
	{
		Container content = getContentPane();
		content.add(mainContainer, BorderLayout.CENTER);
		content.add(buttonContainer, BorderLayout.SOUTH);
		mainContainer.setLayout(new MainLayout());
		mainContainer.add(imageComp);
		mainContainer.add(namePanel);
		mainContainer.add(propertyPanel);
		mainContainer.add(materialPanel);
		namePanel.add(nameLabel);
		namePanel.add(nameField);

		initButtonContainer(buttonContainer);
	}

	private void initListeners()
	{
		save.addActionListener((ActionEvent e) ->
		{
			save();
		});
		cancel.addActionListener((ActionEvent e) ->
		{
			cancel();
		});
	}

	protected void initButtonContainer(Container buttonContainer)
	{
		buttonContainer.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
		buttonContainer.add(cancel);
		buttonContainer.add(save);
	}

	public void updateContent(Image image, String name, File file)
	{
		if (foodData == null)
			foodData = new FoodData(null);
		if (file != null && file.isFile())
			foodData.readFromFile(file);
		else
			foodData.clear();
		foodData.setFoodName(name);
		oldFoodData.setFoodData(foodData);
		imageComp.setImage(image);
		nameField.setText(name);
		propertyPanel.setFoodProperty(foodData.getFoodProperty());
		materialPanel.setFoodMaterial(foodData.getFoodMaterial());
		dataFile = file;
	}

	public String getSettingName()
	{
		String text = nameField.getText();
		if (TextTools.isBlank(text))
			return null;
		return text;
	}

	protected void save()
	{
		if (dataFile != null)
			foodData.writeToFile(dataFile);
		dispose();
	}

	protected void cancel()
	{
		if (dataFile != null)
			oldFoodData.writeToFile(dataFile);
		dispose();
	}

	private class MainLayout implements LayoutManager
	{
		@Override
		public void addLayoutComponent(String name, Component comp)
		{
		}

		@Override
		public void removeLayoutComponent(Component comp)
		{
		}

		@Override
		public Dimension preferredLayoutSize(Container parent)
		{
			return null;
		}

		@Override
		public Dimension minimumLayoutSize(Container parent)
		{
			return null;
		}

		@Override
		public void layoutContainer(Container parent)
		{
			int width = parent.getWidth(), height = parent.getHeight();
			int iW = (int) (width * 0.5f), iH = height - 80;
			float hh = iW * 0.78f;
			if (hh < iH)
				iH = (int) hh;
			else
				iW = (int) (iH / 0.78f);
			imageComp.setBounds(0, 0, iW, iH);
			namePanel.setBounds(0, iH, iW, height - iH);
			int h = (int) (height * 0.4);
			propertyPanel.setBounds(iW, 0, width - iW, h);
			h += 6;
			materialPanel.setBounds(iW, h, width - iW, height - h);
		}
	}
}
